package com.zimmur.app.mocolink.background.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zimmur.app.mocolink.background.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.background.common.StatusCodeEnum;
import com.zimmur.app.mocolink.background.util.PublicKeyUtil;
import com.zimmur.app.mocolink.domain.form.AddressForm;
import com.zimmur.app.mocolink.domain.model.AppUser;
import com.zimmur.app.mocolink.domain.model.UserAddress;
import com.zimmur.app.mocolink.domain.model.UserAdvice;
import com.zimmur.app.mocolink.domain.model.VerifyMobileLog;
import com.zimmur.app.mocolink.domain.util.DateUtils;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.AppUserInfo;
import com.zimmur.app.mocolink.domain.vo.GoodsCollectionInfo;
import com.zimmur.app.mocolink.domain.vo.GroupCollectionInfo;
import com.zimmur.app.mocolink.domain.vo.UserAddressInfo;
import com.zimmur.app.mocolink.domain.vo.UserCommentGoodsInfo;
import com.zimmur.app.mocolink.domain.vo.UserCommentGroupInfo;
import com.zimmur.app.mocolink.service.IAppUserService;
import com.zimmur.app.mocolink.service.IUserAddressService;
import com.zimmur.app.mocolink.service.IUserAdviceService;
import com.zimmur.app.mocolink.service.IUserCollectionService;
import com.zimmur.app.mocolink.service.IUserCommentService;
import com.zimmur.app.mocolink.service.IVerifyMobileService;

@RestController
@RequestMapping("/restful/userCenter")
public class UserCenterController extends BaseController{
	@Autowired
	private IAppUserService appUserService;
	@Autowired
	private PublicKeyUtil publicKeyUtil;
	@Autowired
	private IUserAddressService userAddressService;
	@Autowired
	private IUserAdviceService userAdviceService;
	@Autowired
	private IUserCommentService userCommentService;
	@Autowired
	private IUserCollectionService userCollectionService;
	@Autowired
	private IVerifyMobileService verifyMobileService;
	
	@RequestMapping("/saveUserInfo")
	public ReturnJsonEntity saveUserInfo(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		String headIcon = getParam("headIcon");
		String nickname = getParam("nickname");
		AppUser user = appUserService.selectById(userId);
		if(!StringUtils.isBlank(nickname)){
			user.setNickname(nickname);
		}
		if(!StringUtils.isBlank(headIcon)){
			user.setHeadIcon(headIcon);
		}
		int flag = appUserService.update(user);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryUserInfo")
	public ReturnJsonEntity queryUserInfo(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		AppUserInfo userInfo = appUserService.selectInfoByUserId(userId);
		
		if(userInfo!=null){
			String publicKey = publicKeyUtil.getKey();
			userInfo.setPublicKey(publicKey);
			entity.setData(userInfo);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/saveAddress")
	public ReturnJsonEntity saveAddress(AddressForm addressForm){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId = getTokenUserId();
		int flag=0;
		UserAddress address =new UserAddress();
		address.setAddress(addressForm.getAddress());
		address.setCity(addressForm.getCity());
		address.setContacts(addressForm.getContacts());
		address.setDistrict(addressForm.getDistrict());
		address.setMobileNo(addressForm.getMobileNo());
		address.setProvince(addressForm.getProvince());
		address.setUserId(userId);
		address.setUpdateTime(DateUtils.getNow());
		if(addressForm.getAddressId()!=null && addressForm.getAddressId()>0){
			address.setId(addressForm.getAddressId());
			flag = userAddressService.update(address);
		}else{
			address.setIsDefault((short)0);
			flag = userAddressService.add(address);
		}
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/deleteAddress")
	public ReturnJsonEntity deleteAddress(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer addressId = getIntParam("addressId");
		int flag = userAddressService.delete(addressId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addressList")
	public ReturnJsonEntity addressList(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		List<UserAddressInfo> list = userAddressService.selectByUserId(userId);
		if(list!=null){
			codeEnum = StatusCodeEnum.CODE000000;
			entity.setData(list);
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/defaultAddress")
	public ReturnJsonEntity defaultAddress(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		UserAddressInfo info = userAddressService.selectDefaultAddress(userId);
		if(info!=null){
			codeEnum = StatusCodeEnum.CODE000000;
			entity.setData(info);
		}else{
			codeEnum = StatusCodeEnum.CODE440401;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/setDefaultAddress")
	public ReturnJsonEntity setDefaultAddress(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer addressId = getIntParam("addressId");
		int flag = userAddressService.updateDefault(addressId, userId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addAdvice")
	public ReturnJsonEntity addAdvice(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		String content = getParam("content");
		UserAdvice userAdvice = new UserAdvice();
		userAdvice.setUserId(userId);
		userAdvice.setContent(content);
		userAdvice.setCreateTime(DateUtils.getNow());
		int flag=userAdviceService.add(userAdvice);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addGoodsComment")
	public ReturnJsonEntity addGoodsComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		String content = getParam("content");
		Integer orderDetailId = getIntParam("orderDetailId");
		
		int flag=userCommentService.addGoodsComment(userId, content, orderDetailId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE430401;
		}else if(flag == -2){
			codeEnum = StatusCodeEnum.CODE430403;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/addGroupComment")
	public ReturnJsonEntity addGroupComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		String content = getParam("content");
		Integer groupGoodsId = getIntParam("groupGoodsId");
		
		int flag=userCommentService.addGroupComment(userId, content, groupGoodsId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE430402;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryGoodsComment")
	public ReturnJsonEntity queryGoodsComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer pageNo = getParam("pageNo", 1);
		
		PageUtil<UserCommentGoodsInfo> page = userCommentService.queryGoodsCommetntByUserId(userId, pageNo);
		if(page != null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/queryGroupComment")
	public ReturnJsonEntity queryGroupComment(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer pageNo = getParam("pageNo", 1);
		PageUtil<UserCommentGroupInfo> page = userCommentService.queryGroupCommentByUserId(userId, pageNo);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/deleteCollection")
	public ReturnJsonEntity deleteCollection(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String collectionIds = getParam("collectionIds");
		if(!StringUtils.isBlank(collectionIds)){
			String[] collectIdArray = collectionIds.split(",");
			int flag = userCollectionService.delete(collectIdArray);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/goodsCollection")
	public ReturnJsonEntity goodsCollection(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer pageNo = getParam("pageNo",1);
		PageUtil<GoodsCollectionInfo> page = userCollectionService.queryGoodsByUserId(userId, pageNo);
		
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/groupGoodsCollection")
	public ReturnJsonEntity groupGoodsCollection(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer pageNo = getParam("pageNo",1);
		PageUtil<GroupCollectionInfo> page = userCollectionService.queryGroupByUserId(userId, pageNo);
		if(page!=null){
			entity.setData(page);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/collectGoods")
	public ReturnJsonEntity collectGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer goodsId=getIntParam("goodsId");
		int flag =userCollectionService.collectGoods(goodsId, userId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/collectGroupGoods")
	public ReturnJsonEntity collectGroupGoods(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		Integer groupGoodsId = getParam("groupGoodsId",0);
		Map<String,Integer> map =userCollectionService.collectGroup(groupGoodsId, userId);
		if(map!=null){
			entity.setData(map);
			codeEnum = StatusCodeEnum.CODE000000;
		}
		entity.init(codeEnum);
		return entity;
	}
	@RequestMapping("/updatePassword")
	public ReturnJsonEntity updatePassword(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer userId=getTokenUserId();
		String newpwd = getParam("newpwd");
		String oldpwd = getParam("oldpwd");
		String verifyCode = getParam("verifyCode");
		
		if(StringUtils.isBlank(newpwd,oldpwd,verifyCode)){
			codeEnum = StatusCodeEnum.CODE100000;
		}else{
			oldpwd = oldpwd.toUpperCase();
			newpwd = newpwd.toUpperCase();
			AppUser appUser = appUserService.selectById(userId);
			if(appUser==null){
				codeEnum = StatusCodeEnum.CODE203401;
			}else{
				VerifyMobileLog mobileLog = verifyMobileService.getInfoByMobile(appUser.getMobileNo());
				if(mobileLog!=null && verifyCode.equals(mobileLog.getVerifyCode()) && mobileLog.getIsVerify()==0){
					if(oldpwd.equals(appUser.getPassword())){
						appUser.setAuthToken("");
						appUser.setPassword(newpwd);
						int flag = appUserService.update(appUser);
						if(flag>0){
							codeEnum = StatusCodeEnum.CODE000000;
							verifyMobileService.updateInfoStatus(mobileLog);
						}
					}else{
						codeEnum = StatusCodeEnum.CODE203403;	
					}
				}else{
					codeEnum = StatusCodeEnum.CODE203402;
				}
			}
		}
		entity.init(codeEnum);
		return entity;
	}
}
